package cn.snow.iot.module.cps.controller.admin.equipment;

import cn.snow.iot.framework.common.pojo.CommonResult;
import cn.snow.iot.module.cps.controller.admin.equipment.vo.EquipmentPage;
import cn.snow.iot.module.cps.controller.admin.equipment.vo.EquipmentPageVO;
import cn.snow.iot.module.cps.controller.admin.equipment.vo.SearchParamVO;
import cn.snow.iot.module.cps.dal.dataobject.equipment.EquipmentDO;
import cn.snow.iot.module.cps.service.equipment.EquipmentService;
import cn.snow.iot.module.cps.utils.StringToList;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;


@Tag(name = "管理后台 - 设备管理")
@RestController
@RequestMapping("/cps/equipment")
public class EquipmentController {

    @Resource
    private EquipmentService equipmentService;


    /**
     * 查询自定义分页
     */
    @GetMapping("/page")
    @ApiOperationSupport(order = 1)
    public CommonResult<IPage<EquipmentPage>> page(EquipmentPageVO equipment) {
        IPage<EquipmentPage> pages = equipmentService.selectEquipmentPage(equipment);
        return CommonResult.success(pages);
    }

    /**
     * 批量启用
     */
    @PostMapping("/enableEquipment")
    @ApiOperationSupport(order = 5)
    public CommonResult<Boolean> enableEquipment(@RequestParam String ids) {
        return CommonResult.success(equipmentService.enableEquipment(StringToList.toLongList(ids)));
    }

    /**
     * 批量失效
     */
    @PostMapping("/disenableEquipment")
    @ApiOperationSupport(order = 12)
    public CommonResult<Boolean> disenableEquipment(@RequestParam String ids) {
        return CommonResult.success(equipmentService.disenableEquipment(StringToList.toLongList(ids)));
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @ApiOperationSupport(order = 4)
    public CommonResult<Boolean> remove(@RequestBody EquipmentPage equipment) {
        return CommonResult.success(equipmentService.removeEquipment(equipment));
    }

    /**
     * 重启
     *
     * @return
     */
    @GetMapping("/reStart")
    @ApiOperationSupport(order = 15)
    public CommonResult reStart() {
        equipmentService.restart();
        return CommonResult.success(equipmentService.restart());
    }

    /**
     * 新增或修改
     */
    @PostMapping("/submit")
    @ApiOperationSupport(order = 3)
    public CommonResult submit(@RequestBody EquipmentDO equipment) {
        return equipmentService.submit(equipment);
    }

    /**
     * 详情
     */
    @GetMapping("/detail")
    @ApiOperationSupport(order = 1)
    public CommonResult<EquipmentDO> detail(Long id) {
        return CommonResult.success(equipmentService.getById(id));
    }

    /**
     * 设备数据监控接口，此接口会针对当前设备进行ekuiper规则创建，请勿在其他业务处使用
     */
    @GetMapping("/detailMonitor")
    @ApiOperationSupport(order = 1)
    public CommonResult<EquipmentDO> detailMonitor(Long id) {
        return CommonResult.success(equipmentService.detailMonitor(id));
    }


    /**
     * 根据产品编号查询设备
     *
     * @param equipment equipment实体
     * @return CommonResult<IPage<EquipmentDO>>
     */
    @GetMapping("/pageByProductCode")
    @ApiOperationSupport(order = 1)
    public CommonResult<IPage<EquipmentPage>> pageByProductCode(EquipmentPageVO equipment) {
        IPage<EquipmentPage> pages = equipmentService.selectEquipmentPageByProductCode(equipment);
        return CommonResult.success(pages);
    }

    @GetMapping("/cancelPassage")
    @ApiOperationSupport(order = 1)
    public CommonResult cancelPassage(String id) {
        return equipmentService.cancelPassage(id);
    }

    /**
     * 设备历史数据查询
     *
     * @param searchParamVO 传入开始时间，结束时间
     * @param distinction   月份等等的判断
     * @return 返回的数据
     */
    @GetMapping("/selectData")
    @ApiOperationSupport(order = 17)
    @Operation(summary = "今日，本周，本月，开始到结束")
    public CommonResult selectData(SearchParamVO searchParamVO, String distinction) {
        return CommonResult.success(equipmentService.cardDate(searchParamVO, distinction));
    }
}
